package com.yg.controller;


import com.yg.config.result.R;
import com.yg.entity.singleEntity.TeacherAchievementAward;
import com.yg.entity.singleEntity.TechnologyCompetition;
import com.yg.service.TeacherAchievementAwardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * <p>
 * 对应sheet07，是教学成果和教学奖励表 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@Slf4j
@RestController
@Api(tags = "对应第7张表")
@RequestMapping("/teacherAchievementAward")
@CrossOrigin //使其支持跨域资源共享
public class TeacherAchievementAwardController {

    @Autowired
    private TeacherAchievementAwardService service;

    /**
     * 读取excel写入数据库
     * clr
     *
     * @param file 上传的文件
     * @return 统一返回R类
     */
    @ApiOperation(value = "上传文件")
    @PostMapping("/readExcel")
    public R readExcel(@ApiParam(value = "文件上传") MultipartFile file) {
        log.info("执行了teacher-achievement-award的readExcel方法");

        return service.readExcel(file);
    }

    /**
     * 导出excel
     * clr
     *
     * @return 统一返回R类
     */
    @ApiOperation(value = "下载包含所有数据的excel")
    @GetMapping("/writeExcel")
    public R writeExcel(@ApiParam(value = "excel下载") HttpServletResponse response) {
        return service.writeExcel(response);
    }

    /**
     * 分页查询
     *
     * @param currentPage 当前页码
     * @param limit       每页需要呈现多少数据
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/selectAllByPage")
    public R selectAllByPage(@ApiParam(value = "当前页码") @RequestParam(value = "currentPage") int currentPage,
                             @ApiParam(value = "每页要展现的数据量") @RequestParam(value = "limit") int limit) {

        return service.selectPage(currentPage, limit);

    }

    /**
     * 从网页获取数据并插入到数据库
     *
     * @param entity 使用TeacherAchievementAward 封装从前端传入的表单数据
     * @return 返回影响的行数
     */
    @ApiOperation(value = "插入数据")
    @PostMapping("/insert")
    public R insert(@ApiParam(value = "用户要新增的表单数据") @RequestBody TeacherAchievementAward entity) {
        return service.insertOrUpdate(entity);
    }

    /**
     * 从网页获取数据并更新到数据库
     *
     * @param entity 使用 TeacherAchievementAward 封装从前端传入的表单数据
     * @return 返回影响的行数
     */
    @ApiOperation(value = "更新数据")
    @PutMapping("/update")
    public R update(@ApiParam(value = "用户修改好的表单数据") @RequestBody TeacherAchievementAward entity) {
        return service.insertOrUpdate(entity);

    }

    /**
     * 删除数据
     *
     * @param teacherAchievementAward 包含有主键id的实体类
     * @return 返回影响的行数
     */

    @ApiOperation(value = "根据id删除数据")
    @DeleteMapping("/deleteById")
    public R deleteById(@ApiParam(value = "传入的id") @RequestBody TeacherAchievementAward teacherAchievementAward) {
        return service.deleteById(teacherAchievementAward);
    }


    /**
     * 模糊查询
     *
     * @param currentPage 当前页码
     * @param limit       每页需要呈现的数据条数
     * @param entity      使用entity接受模糊查询的条件参数
     * @return 统一返回R类
     */
    @ApiOperation(value = "模糊查询")
    @PostMapping("/selectByCondition")
    R selectByCondition(@ApiParam(value = "当前页码") @RequestParam(value = "currentPage") int currentPage,
                        @ApiParam(value = "每页要展现的数据量") @RequestParam(value = "limit") int limit,
                        @ApiParam(value = "模糊查询所需要的属性名") @RequestBody(required = false) TeacherAchievementAward entity) {
        if (entity == null) {
            entity = new TeacherAchievementAward();
        }
        return service.selectByCondition(currentPage, limit, entity);
    }


    /**
     * @param rewardName         模糊查询的条件参数
     * @param achievementName    模糊查询的条件参数
     * @param awardProvinceLevel 模糊查询的条件参数
     * @param awardSchoolLevel   模糊查询的条件参数
     * @param response           response
     * @return 统一返回R类
     */
    @ApiOperation(value = "模糊查询后导出")
    @GetMapping("/writeByCondition")
    R writeByCondition(@ApiParam(value = "模糊查询所需要的属性名") @RequestParam(required = false) String rewardName,
                       @ApiParam(value = "模糊查询所需要的属性名") @RequestParam(required = false) String achievementName,
                       @ApiParam(value = "模糊查询所需要的属性名") @RequestParam(required = false) String awardProvinceLevel,
                       @ApiParam(value = "模糊查询所需要的属性名") @RequestParam(required = false) String awardSchoolLevel,
                       HttpServletResponse response) {
        TeacherAchievementAward entity = new TeacherAchievementAward();
        entity.setRewardName(rewardName);
        entity.setAchievementName(achievementName);
        entity.setAwardSchoolLevel(awardSchoolLevel);
        entity.setAwardProvinceLevel(awardProvinceLevel);
        log.error(entity.toString());
        return service.writeByCondition(entity, response);
    }

}

